package com.github.skystardust.lotteryex;

import com.github.skystardust.lotteryex.commands.LotteryRunCommand;
import com.github.skystardust.lotteryex.config.RewardConfig;
import com.github.skystardust.lotteryex.hooks.PlayerPointsHook;
import com.github.skystardust.lotteryex.hooks.VaultHook;
import com.github.skystardust.lotteryex.lottery.LotteryItem;
import com.github.skystardust.lotteryex.lottery.bundle.AwardBundle;
import com.github.skystardust.lotteryex.lottery.bundle.LotteryBundle;
import com.github.skystardust.lotteryex.lottery.bundle.RequireBundle;
import com.github.skystardust.lotteryex.lottery.bundle.profile.LotteryProfile;
import com.github.skystardust.lotteryex.lottery.item.imp.award.*;
import com.github.skystardust.lotteryex.lottery.item.imp.require.RequireItemItem;
import com.github.skystardust.lotteryex.lottery.item.imp.require.RequireItemMoney;
import com.github.skystardust.lotteryex.lottery.item.imp.require.RequireItemPermission;
import com.github.skystardust.lotteryex.lottery.item.imp.require.RequireItemPoints;
import com.github.skystardust.lotteryex.utils.MessageUtils;
import com.github.skystardust.lotteryex.utils.OptionalUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.java.JavaPlugin;

public final class LotteryEX extends JavaPlugin {
    private static final String PLUGIN_PREFIX = "§a§l[LotteryEX] §r";
    private static LotteryEX lotteryEX;
    private VaultHook vaultHook;
    private PlayerPointsHook playerPointsHook;
    private RewardConfig rewardConfig;

    public static LotteryEX getLotteryEX() {
        return lotteryEX;
    }

    public static String getPluginPrefix() {
        return PLUGIN_PREFIX;
    }

    @Override
    public void onLoad() {
        ConfigurationSerialization.registerClass(AwardBundle.class);
        ConfigurationSerialization.registerClass(RequireBundle.class);
        ConfigurationSerialization.registerClass(AwardItemItem.class);
        ConfigurationSerialization.registerClass(AwardItemCommand.class);
        ConfigurationSerialization.registerClass(AwardItemMoney.class);
        ConfigurationSerialization.registerClass(AwardItemPermission.class);
        ConfigurationSerialization.registerClass(AwardItemPoints.class);
        ConfigurationSerialization.registerClass(RequireItemPermission.class);
        ConfigurationSerialization.registerClass(RequireItemItem.class);
        ConfigurationSerialization.registerClass(RequireItemMoney.class);
        ConfigurationSerialization.registerClass(RequireItemPermission.class);
        ConfigurationSerialization.registerClass(RequireItemPoints.class);
        ConfigurationSerialization.registerClass(LotteryItem.class);
        ConfigurationSerialization.registerClass(AwardBundle.class);
        ConfigurationSerialization.registerClass(RequireBundle.class);
        ConfigurationSerialization.registerClass(LotteryProfile.class);
        ConfigurationSerialization.registerClass(LotteryBundle.class);
    }

    @Override
    public void onEnable() {
        LotteryEX.lotteryEX = this;
        vaultHook = new VaultHook();
        new OptionalUtils<>(() -> vaultHook.setupEconomy()).getOptional().ifPresent(success -> {
            if (success) {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .info("挂钩Vault Economy成功!");
            } else {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .error("挂钩Vault Economy失败!");
            }
        });
        new OptionalUtils<>(() -> vaultHook.setupPermission()).getOptional().ifPresent(success -> {
            if (success) {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .info("挂钩Vault Permission成功!");
            } else {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .error("挂钩Vault Permission失败!");
            }
        });
        this.playerPointsHook = new PlayerPointsHook();
        new OptionalUtils<>(() -> playerPointsHook.setupPlayerPoints()).getOptional().ifPresent(success -> {
            if (success) {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .info("挂钩PlayerPoints成功!");
            } else {
                new MessageUtils<>(Bukkit.getConsoleSender())
                        .error("挂钩PlayerPoints失败!");
                this.playerPointsHook = null;
            }
        });
        getServer().getPluginCommand("lex").setExecutor(new LotteryRunCommand());
        this.rewardConfig = new RewardConfig("reward.yml", this);
    }

    @Override
    public void onDisable() {

    }

    public VaultHook getVaultHook() {
        return vaultHook;
    }

    public PlayerPointsHook getPlayerPointsHook() {
        return playerPointsHook;
    }

    public RewardConfig getRewardConfig() {
        return rewardConfig;
    }
}
